﻿using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Text;


namespace Skewworks.Win.Motif.Glide
{
    public class GlideFont
    {

        [DllImport("gdi32.dll")]
        private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);


        private static Font fnt8, fnt9, fnt10, fnt11, fnt12, fnt14, fnt16, fnt18, fnt20, fnt24;

        public static Font DroidSans(int fontSize)
        {
            switch (fontSize)
            {
                case 8:
                    if (fnt8 == null)
                        fnt8 = LoadFont(fontSize);
                    return fnt8;
                case 9:
                    if (fnt9 == null)
                        fnt9 = LoadFont(fontSize);
                    return fnt9;
                case 10:
                    if (fnt10 == null)
                        fnt10 = LoadFont(fontSize);
                    return fnt10;
                case 11:
                    if (fnt11 == null)
                        fnt11 = LoadFont(fontSize);
                    return fnt11;
                case 12:
                    if (fnt12 == null)
                        fnt12 = LoadFont(fontSize);
                    return fnt12;
                case 14:
                    if (fnt14 == null)
                        fnt14 = LoadFont(fontSize);
                    return fnt14;
                case 16:
                    if (fnt16 == null)
                        fnt16 = LoadFont(fontSize);
                    return fnt16;
                case 18:
                    if (fnt18 == null)
                        fnt18 = LoadFont(fontSize);
                    return fnt18;
                case 20:
                    if (fnt20 == null)
                        fnt20 = LoadFont(fontSize);
                    return fnt20;
                default:
                    if (fnt24 == null)
                        fnt24 = LoadFont(fontSize);
                    return fnt24;
            }


        }

        private static PrivateFontCollection MYpfc = new PrivateFontCollection();

        private static Font LoadFont(int fontSize)
        {

            unsafe
            {
                fixed (byte* pFontData = Resources.DroidSans)
                {
                    uint dummy = 0;
                    MYpfc.AddMemoryFont((IntPtr)pFontData, Resources.DroidSans.Length);
                    AddFontMemResourceEx((IntPtr)pFontData, (uint)Resources.DroidSans.Length, IntPtr.Zero, ref dummy);
                }
            }

            return new Font(MYpfc.Families[0], fontSize);
        }

    }
}
